home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Utilities / Cron ƒ / cron commons / argc Receiver.p < prev   
Encoding:
Text File  |  1993-07-04  |  7.5 KB  |  359 lines  |  [TEXT/PJMM]

  1. unit argcReceiver;
  2.  
  3. interface
  4.  
  5.     type
  6.         StringPtrArray = array[0..1000] of StringPtr;
  7.         StringPtrArrayPtr = ^StringPtrArray;
  8.  
  9.     procedure argcReceiver (var argcRcvd: integer; var argvRcvd: StringPtrArrayPtr);
  10.     function TrapAvailable (theTrap: integer): boolean;
  11.     procedure MacInits;
  12.  
  13. {    ——————————————————————————————————————————————————————————————————}
  14. {    }
  15. {    Filename: "argc Receiver.c"}
  16. {    argc/argv argument receiver code, written for THINK C 5.0}
  17. {    By Chris Johnson}
  18. {    Version of: Thursday, July 23, 1992}
  19. {    }
  20. {    Distribute freely and without charge, but say something nice about }
  21. {    the author when you use it.  Please send me a copy of any improve-}
  22. {    ments you make so they can be incorporated into future versions.}
  23. {    }
  24. {    ——————————————————————————————————————————————————————————————————}
  25. {    Internet:    chrisj@emx.utexas.edu}
  26. {    UUCP:        (husc6|uunet)!cs.utexas.edu!ut-emx!chrisj}
  27. {    BitNet:        chrisj@utxvm.bitnet}
  28. {    AppleLink:    chrisj@emx.utexas.edu@internet#}
  29. {    CompuServe:    >INTERNET:chrisj@emx.utexas.edu}
  30. {    US Mail:    Chris Johnson, 4505-B Avenue H, Austin, TX 78751}
  31. {    —————————————————————————————————————————————————————————————————-}
  32.  
  33. implementation
  34.  
  35.     uses
  36.         Traps, AppleEvents;
  37.  
  38.     const
  39.         argcEventClass = 'args';
  40.         argcEventID = 'argc';
  41.         argcKeyword = 'argc';
  42.  
  43.     var
  44.         WaitFlag: Boolean;
  45.         argc: integer;
  46.         argv: StringPtrArrayPtr;
  47.  
  48.     function NumToolboxTraps: integer;
  49.     begin
  50.         if NGetTrapAddress(_InitGraf, ToolTrap) = NGetTrapAddress($AA6E, ToolTrap) then begin
  51.             NumToolboxTraps := $200;
  52.         end
  53.         else begin
  54.             NumToolboxTraps := $400;
  55.         end;
  56.     end;
  57.  
  58.     function GetTrapType (theTrap: integer): TrapType;
  59.         var
  60.             tType: TrapType;
  61.     begin
  62.  
  63.         if BAND(theTrap, $0800) <> 0 then begin
  64.             GetTrapType := ToolTrap;
  65.         end
  66.         else begin
  67.             GetTrapType := OSTrap;
  68.         end;
  69.     end;
  70.  
  71.  
  72.  
  73.     function TrapAvailable (theTrap: integer): Boolean;
  74.         var
  75.             tType: TrapType;
  76.     begin
  77.  
  78.         tType := GetTrapType(theTrap);
  79.  
  80.         if tType = ToolTrap then begin
  81.  
  82.             theTrap := BAND(theTrap, $07FF);
  83.             if theTrap >= NumToolboxTraps then begin
  84.                 theTrap := _Unimplemented;
  85.             end;
  86.         end;
  87.  
  88.         TrapAvailable := NGetTrapAddress(theTrap, tType) <> NGetTrapAddress(_Unimplemented, ToolTrap);
  89.     end;
  90.  
  91.     function MyGotRequiredParams (Event: AppleEvent): OSErr;
  92.         var
  93.             OSError: OSErr;
  94.             ReturnedType: DescType;
  95.             ActualSize: longInt;
  96.     begin
  97.  
  98.         OSError := AEGetAttributePtr(Event, keyMissedKeywordAttr, typeWildCard, ReturnedType, nil, 0, ActualSize);
  99.         if OSError = errAEDescNotFound then begin
  100.  
  101.             OSError := noErr;
  102.  
  103.         end
  104.         else if OSError = noErr then begin
  105.             OSError := errAEEventNotHandled;
  106.         end;
  107.  
  108.         MyGotRequiredParams := OSError;
  109.     end;
  110.  
  111.  
  112.     function Quit (var RcvdEvent: AppleEvent; var ReplyEvent: AppleEvent; RefCon: longInt): OSErr;
  113.         var
  114.             OSError: OSErr;
  115.     begin
  116.  
  117.         OSError := MyGotRequiredParams(RcvdEvent);
  118.         if OSError = noErr then begin
  119.             WaitFlag := FALSE;
  120.         end;
  121.  
  122.         Quit := OSError;
  123.     end;
  124.  
  125.     function ArgsGet (RcvdEvent: AppleEvent; ReplyEvent: AppleEvent; RefCon: longInt): OSErr;
  126.         var
  127.             OSError, junk: OSErr;
  128.             ArgCList: AEDesc;
  129.             ArgCount: longInt;
  130.             Keyword: AEKeyword;
  131.             ActualSize: longInt;
  132.             CurArgV: integer;
  133.             DataType: DescType;
  134.             DataSize: longInt;
  135.     begin
  136.         OSError := AEGetParamDesc(RcvdEvent, argcKeyword, typeAEList, ArgCList);
  137.         if OSError = noErr then begin
  138.  
  139.             OSError := AECountItems(ArgCList, ArgCount);
  140.             if OSError = noErr then begin
  141.  
  142.                 argc := ArgCount;
  143.                 argv := StringPtrArrayPtr(NewPtrClear(sizeof(Ptr) * (ArgCount + 1)));
  144.  
  145.                 OSError := MemError;
  146.                 if OSError = noErr then begin
  147.  
  148.                     for CurArgV := 0 to ArgCount - 1 do begin
  149.  
  150.                         OSError := AESizeOfNthItem(ArgCList, CurArgV + 1, DataType, DataSize);
  151.                         if OSError = noErr then begin
  152.  
  153.                             argv^[CurArgV] := StringPtr(NewPtr(DataSize + 1));
  154.                             OSError := MemError;
  155.                             if OSError = noErr then begin
  156.  
  157. {$PUSH}
  158.  {$R-}
  159.                                 OSError := AEGetNthPtr(ArgCList, CurArgV + 1, typeChar, Keyword, DataType, @argv^[CurArgV]^[1], DataSize, ActualSize);
  160.                                 if OSError = noErr then begin
  161.                                     argv^[CurArgV]^[0] := chr(DataSize);
  162. {$POP}
  163.                                 end;
  164.                             end;
  165.                         end;
  166.                         if OSError <> noErr then
  167.                             leave;
  168.                     end;
  169.  
  170.                 end;
  171.             end;
  172.  
  173.             junk := AEDisposeDesc(ArgCList);
  174.         end;
  175.  
  176.         if OSError = noErr then begin
  177.             OSError := MyGotRequiredParams(RcvdEvent);
  178.         end;
  179.  
  180.         if OSError <> noErr then begin
  181.  
  182.             argc := 0;
  183.             argv := nil;
  184.         end;
  185.  
  186.         WaitFlag := FALSE;
  187.  
  188.         ArgsGet := OSError;
  189.     end;
  190.  
  191.     procedure ArgsDispose;
  192.         var
  193.             CurArg: integer;
  194.     begin
  195.         if argv <> nil then begin
  196.  
  197.             for CurArg := 0 to argc - 1 do begin
  198.  
  199.                 if argv^[CurArg] <> nil then begin
  200.                     DisposPtr(Ptr(argv^[CurArg]));
  201.                 end;
  202.             end;
  203.  
  204.             DisposPtr(Ptr(argv));
  205.         end;
  206.  
  207.         argc := 0;
  208.         argv := nil;
  209.     end;
  210.  
  211.  
  212.     function EnvironmentCheck: boolean;
  213.         var
  214.             Continue: boolean;
  215.             Response: longInt;
  216.     begin
  217.         Continue := FALSE;
  218.  
  219.         if TrapAvailable(_WaitNextEvent) then begin
  220.  
  221.             if TrapAvailable(_GestaltDispatch) then begin
  222.  
  223.                 if Gestalt(gestaltAppleEventsAttr, Response) = noErr then begin
  224.  
  225.                     if BTST(Response, gestaltAppleEventsPresent) then begin
  226.  
  227.                         if AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, @Quit, 0, FALSE) = noErr then begin
  228.  
  229.                             if AEInstallEventHandler(argcEventClass, argcEventID, @ArgsGet, 0, FALSE) = noErr then begin
  230.  
  231.                                 WaitFlag := TRUE;
  232.                                 Continue := TRUE;
  233.                             end;
  234.                         end;
  235.                     end;
  236.                 end;
  237.             end;
  238.         end;
  239.  
  240.         EnvironmentCheck := Continue;
  241.     end;
  242.  
  243.  
  244. {    ——————————————————————————————————————————————————————————  }
  245. {    All of the following code comes from IM VI pg. 3-8. }
  246. {    ——————————————————————————————————————————————————————————    }
  247.  
  248.  
  249.  
  250. {    ——————————————————————————————————————————————————————————  }
  251. {    Code to determine what sort of toolbox initializations we   }
  252. {    need to perform. }
  253. {    ——————————————————————————————————————————————————————————    }
  254.  
  255.     const
  256.         BackgroundOnlyMask = $0400;
  257.  
  258.     function BackgroundOnlyApp: boolean;
  259.         type
  260.             intPtr = ^integer;
  261.             intHandle = ^intPtr;
  262.         var
  263.             BackgroundOnly: Boolean;
  264.             SizeHand: handle;
  265.             Flags: integer;
  266.     begin
  267.         BackgroundOnly := FALSE;
  268.  
  269.         SizeHand := Get1Resource('SIZE', -1);
  270.         if SizeHand <> nil then begin
  271.  
  272.             LoadResource(SizeHand);
  273.             if ResError = noErr then begin
  274.  
  275.                 Flags := intHandle(SizeHand)^^;
  276.  
  277.                 if BAND(Flags, BackgroundOnlyMask) <> 0 then begin
  278.  
  279.                     BackgroundOnly := TRUE;
  280.                 end;
  281.             end;
  282.  
  283.             ReleaseResource(SizeHand);
  284.         end;
  285.  
  286.         BackgroundOnlyApp := BackgroundOnly;
  287.     end;
  288.  
  289.     procedure MacInits;
  290.     begin
  291.  
  292.         InitGraf(@thePort);
  293.         if BackgroundOnlyApp = FALSE then begin
  294.  
  295.             InitFonts;
  296.             InitWindows;
  297.             InitMenus;
  298.             TEInit;
  299.             InitDialogs(nil);
  300.             InitCursor;
  301.         end;
  302.     end;
  303.  
  304.     procedure argcReceiver (var argcRcvd: integer; var argvRcvd: StringPtrArrayPtr);
  305.         var
  306.             TimeoutTicks: longInt;
  307.             Event: EventRecord;
  308.             junk: OSErr;
  309.     begin
  310.  
  311.         ArgsDispose;
  312.         if EnvironmentCheck then begin
  313.  
  314.             TimeoutTicks := TickCount + 60 * 60;
  315.  
  316.             while WaitFlag do begin
  317.  
  318.                 if WaitNextEvent(everyEvent, Event, 60, nil) then begin
  319.  
  320.                     case Event.what of
  321.  
  322.                         keyDown:  begin
  323.  
  324.                             if BAND(Event.modifiers, cmdKey) <> 0 then begin
  325.  
  326.                                 if chr(BAND(Event.message, $FF)) = 'q' then begin
  327.                                     WaitFlag := FALSE;
  328.                                 end;
  329.                             end;
  330.                         end;
  331.  
  332.                         kHighLevelEvent:  begin
  333.                             junk := AEProcessAppleEvent(Event);
  334.                         end;
  335.                     end; { case }
  336.                 end; { if }
  337.  
  338.         {    If we're still waiting and we've been waiting more than  }
  339.         {    a minute, it's time to quit. }
  340.  
  341.                 if TickCount >= TimeoutTicks then begin
  342.                     WaitFlag := FALSE;
  343.                 end;
  344.             end;
  345.  
  346.             junk := AERemoveEventHandler(kCoreEventClass, kAEQuitApplication, @Quit, FALSE);
  347.             junk := AERemoveEventHandler(argcEventClass, argcEventID, @ArgsGet, FALSE);
  348.         end;
  349.  
  350.         if argv = nil then begin
  351.             ExitToShell;
  352.         end;
  353.  
  354.         argcRcvd := argc;
  355.         argvRcvd := argv;
  356.     end;
  357.  
  358.  
  359. end.